package cn.fzkj.framework.messagequece.consume;

import cn.fzkj.common.constant.HttpStatus;
import cn.fzkj.framework.messagequece.MessageInfo;
import cn.fzkj.framework.messagequece.MessageType;
import cn.fzkj.framework.messagequece.config.RabbitConfig;
import cn.fzkj.framework.web.domain.ResultVO;
import cn.fzkj.project.tool.mail.MailUtils;
import com.google.gson.Gson;
import com.rabbitmq.client.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**  邮件消息 消费者 */
@Component
@RabbitListener(queues = { RabbitConfig.QUEUE_MAIL_SERVER })
public class MailConsume {

    private static final Logger log = LoggerFactory.getLogger(RabbitRecevier.class);

    @Autowired
    private MailUtils mailUtils;

    @RabbitHandler
    public void process(String content, Channel channel, Message message) throws IOException {
        // 拿到生产者中生成的uuid【消息的唯一id】
        String uuid = (String)message.getMessageProperties().getHeaders().get("spring_returned_message_correlation");

        log.error("接收到消息，消息id: {} => ：{}", uuid, content);
        MessageInfo info = new Gson().fromJson(content, MessageInfo.class);
        log.error("请求类型：-> {}", info.getMessageType());
        try{
            if (info.getMessageType() == MessageType.MAIL_SIMPLE){ // 处理简单邮件发送
                ResultVO resultVO = mailUtils.sendSimpleMail(info.getContent());
                if ((Integer) resultVO.get("code") == HttpStatus.SUCCESS){
                    // 确认消息
                    channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
                    log.info("ws rabbitmq consume msg： " + message.getMessageProperties().getDeliveryTag());
                }
            } else if (info.getMessageType() == MessageType.MAIL_MIME) { // 处理复杂邮件发送
                log.error("收到消息，开始发送复杂邮件。。。");
                ResultVO resultVO = mailUtils.sendMimeMail(info.getContent());
                if ((Integer) resultVO.get("code") == HttpStatus.SUCCESS){
                    // 确认消息
                    channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
                    log.info("ws rabbitmq consume msg： " + message.getMessageProperties().getDeliveryTag());
                }
            } else {
                // 否认消息
                // 被否认的消息会重新放到队列中
                // 也可以使用 channel.basicReject()来绝收消息，被绝收的消息不会被重新放到队列
                if ((System.currentTimeMillis() - info.getCreateTime()) > 3000){
                    // 3s消息还没有被确认，手动确认，防止一直不停的重复发
                    channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
                    log.error("3s ws rabbit not consume msg : {}", message.getMessageProperties().getDeliveryTag());
                }
                channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
                log.info(" ws rabbitmq in queue again： " + content);
            }
        }catch(Exception e) {
            log.error("mailconsume fail -> {}", e.getMessage());
        }
    }


}
